import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/event/refresh_home_page_event.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/utils/event_bus_utils.dart';
import 'package:nyh_motorcade_app/utils/sp_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';

class RapidTransferController extends GetxController {
  TextEditingController amountController = TextEditingController();
  TextEditingController remarksController = TextEditingController();
  FocusNode amountFocus = FocusNode();
  FocusNode remarksFocus = FocusNode();

  String transferForm = "";

  String transferInAccountType = "";
  String transferOutAccountType = "";

  String transferInAccountId = "";
  String transferInAccountName = "";
  String transferOutAccountId = "";
  String transferOutAccountName = "";

  double transferOutBeforeAmount = 0.0;
  double transferOutAfterAmount = 0.0;
  double transferInBeforeAmount = 0.0;
  double transferInAfterAmount = 0.0;

  double transferAmount = 0.0;

  String remarks = "";

  @override
  void onReady() {
    super.onReady();
    initData();
  }

  Future<void> initData() async {
    dynamic arguments = Get.arguments;
    if (arguments != null && arguments is Map) {
      transferForm = arguments[Constant.PARAM_RAPID_TRANSFER_FROM];
      if (transferForm == Constant.RAPID_TRANSFER_FROM_MOTORCADE || transferForm == Constant.RAPID_TRANSFER_FROM_CARD) {
        transferOutAccountId = await SPUtils.getString(SPUtils.COMPANY_ID);
        transferOutAccountName = await SPUtils.getString(SPUtils.COMPANY_NAME);
        transferOutBeforeAmount = await SPUtils.getDouble(SPUtils.COMPANY_AMOUNT);
        transferOutAfterAmount = transferOutBeforeAmount;

        transferInAccountId = arguments[Constant.PARAM_ACCOUNT_ID];
        transferInAccountName = arguments[Constant.PARAM_ACCOUNT_NAME];
        transferInBeforeAmount = arguments[Constant.PARAM_ACCOUNT_AMOUNT];
        transferInAfterAmount = transferInBeforeAmount;

        transferOutAccountType = Constant.COMPANY;
        if (transferForm == Constant.RAPID_TRANSFER_FROM_MOTORCADE) {
          transferInAccountType = Constant.MOTORCADE;
        } else if (transferForm == Constant.RAPID_TRANSFER_FROM_CARD) {
          transferInAccountType = Constant.CARD;
        }

        update();
      }
    }
  }

  Future<void> selectTransferOutAccount() async {
    bool isPermission = await verifyTransferPermission();
    if (!isPermission) {
      ToastUtils.toast("没有划拨权限");
      return;
    }
    amountFocus.unfocus();
    remarksFocus.unfocus();
    dynamic data = await Get.toNamed(Routes.selectTransferAccount, arguments: false);
    if (data != null && data is Map) {
      String accountId = data[Constant.PARAM_ACCOUNT_ID];
      if (transferOutAccountId != accountId) {
        transferOutAccountId = accountId;
        transferOutAccountName = data[Constant.PARAM_ACCOUNT_NAME];
        transferOutBeforeAmount = data[Constant.PARAM_ACCOUNT_AMOUNT];
        transferOutAccountType = data[Constant.PARAM_ACCOUNT_TYPE];
        transferOutAfterAmount = transferOutBeforeAmount - transferAmount;
        update();
      }
    }
  }

  Future<void> selectTransferInAccount() async {
    bool isPermission = await verifyTransferPermission();
    if (!isPermission) {
      ToastUtils.toast("没有划拨权限");
      return;
    }
    amountFocus.unfocus();
    remarksFocus.unfocus();
    dynamic data = await Get.toNamed(Routes.selectTransferAccount, arguments: true);
    if (data != null && data is Map) {
      String accountId = data[Constant.PARAM_ACCOUNT_ID];
      if (transferInAccountId != accountId) {
        transferInAccountId = accountId;
        transferInAccountName = data[Constant.PARAM_ACCOUNT_NAME];
        transferInBeforeAmount = data[Constant.PARAM_ACCOUNT_AMOUNT];
        transferInAccountType = data[Constant.PARAM_ACCOUNT_TYPE];
        transferInAfterAmount = transferInBeforeAmount + transferAmount;
        update();
      }
    }
  }

  Future<bool> verifyTransferPermission() async {
    bool permissionTransferCompany = await SPUtils.getBool(SPUtils.PERMISSION_TRANSFER_COMPANY);
    bool permissionTransferMotorcade = await SPUtils.getBool(SPUtils.PERMISSION_TRANSFER_MOTORCADE);
    bool permissionTransferCard = await SPUtils.getBool(SPUtils.PERMISSION_TRANSFER_CARD);
    return permissionTransferCompany || permissionTransferMotorcade || permissionTransferCard;
  }

  void setTransferAmount(String value) {
    if (value.isEmpty) {
      transferAmount = 0.0;
    } else {
      transferAmount = double.parse(value);
    }

    if (transferOutAccountId.isNotEmpty) {
      transferOutAfterAmount = transferOutBeforeAmount - transferAmount;
    }
    if (transferInAccountId.isNotEmpty) {
      transferInAfterAmount = transferInBeforeAmount + transferAmount;
    }

    update();
  }

  void setRemarks(String value) {
    remarks = value;
  }

  Future<void> onConfirm() async {
    if (transferInAccountId.isEmpty || transferOutAccountId.isEmpty || transferAmount <= 0.0) {
      return;
    }

    if (transferInAccountId == transferOutAccountId) {
      ToastUtils.toast("拨入账户不能和拨出账户一样");
      return;
    }

    if (transferOutBeforeAmount <= 0.0) {
      ToastUtils.toast("拨出账户余额不足");
      return;
    }

    if (transferAmount > transferOutBeforeAmount) {
      ToastUtils.toast("划拨余额不能大于拨出账户余额");
      return;
    }
    amountFocus.unfocus();
    remarksFocus.unfocus();

    httpManager.post(
      Get.context,
      HttpAddress.ACCOUNT_TRANSFER,
      {
        "superId": await SPUtils.getString(SPUtils.COMPANY_ID),
        "amount": transferAmount,
        "desc": remarks,
        "sourceId": transferOutAccountId,
        "sourceAccountType": transferOutAccountType,
        "targetId": transferInAccountId,
        "targetAccountType": transferInAccountType,
      },
      isToast: true,
      isLoadingDialog: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          return;
        }
        if (data is bool) {
          if (!data) {
            ToastUtils.toast("划拨失败");
            return;
          }
          ToastUtils.toast("划拨成功");
          EventBusUtils.fire(RefreshHomePageEvent());
          Get.back(result: {});
        }
      },
    );
  }

  bool isComplete() {
    return transferInAccountId.isNotEmpty && transferOutAccountId.isNotEmpty && transferAmount > 0.0;
  }
}
